Column

Top 20 ice cream products quantity ordered

data("instacart")

instacart <- 
  instacart |> 
  as_tibble()

instacart |> 
  filter(str_detect(product_name, "Ice Cream")) |> 
  group_by(product_name)  |> 
  summarise(count = n())  |> 
  top_n(20, count) |> 
  mutate( product_name= gsub("Ice Cream", "", product_name)) |> 
  plot_ly(x = ~fct_reorder(product_name, count), y = ~count, type = "bar", color= "darkgreen", colors = "viridis") |> 
  layout(xaxis = list(title = "Ice Cream Name", tickangle = -45), 
         yaxis = list(title = "Count"))

Column

Ice cream orders through the day, by weekday

instacart |> 
  filter(str_detect(product_name, "Ice Cream")) |> 
  group_by(order_hour_of_day, order_dow)  |> 
  summarise(count = n()) |> 
  mutate(weekday = case_match(
    order_dow, 
    0 ~ "Sunday", 
    1 ~ "Monday", 
    2 ~ "Tuesday", 
    3 ~ "Wednesday", 
    4 ~ "Thursday", 
    5 ~ "Friday", 
    6 ~ "Saturday"
    )) |> 
  plot_ly(y = ~count, x = ~order_hour_of_day, type = "scatter", mode= "line", line = list(shape = "spline"), color = ~weekday, colors = "viridis") |> 
  layout(xaxis = list(title = "Order Hour of Day", tickangle = -45), 
         yaxis = list(title = "Units Ice Cream Ordered"))

Order size compared to when ice cream was added to cart

plot_cart <- instacart |> 
  group_by(order_id) |> 
  mutate(total_items = n()) |> 
  filter(str_detect(product_name, "Ice Cream")) |> 
  ggplot(aes(x = total_items,
             y = add_to_cart_order)) +
  geom_point(color= "darkorchid4", alpha = 0.5) +
  xlab("Total items in order") +  
  ylab("Order of Ice Cream to Cart") + 
  theme(legend.position = "none")


ggplotly(plot_cart)
---
title: "Ice Cream Analysis"
output: 
  flexdashboard::flex_dashboard:
    theme: sandstone
    orientation: columns
    vertical_layout: fill
    source: embed
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(p8105.datasets)
library(plotly)
library(viridis)

options(
  ggplot2.continuous.colour = "viridis",
  ggplot2.continuous.fill = "viridis"
)

```

## Column {data-width="650"}

### Top 20 ice cream products quantity ordered

```{r, echo=TRUE}

data("instacart")

instacart <- 
  instacart |> 
  as_tibble()

instacart |> 
  filter(str_detect(product_name, "Ice Cream")) |> 
  group_by(product_name)  |> 
  summarise(count = n())  |> 
  top_n(20, count) |> 
  mutate( product_name= gsub("Ice Cream", "", product_name)) |> 
  plot_ly(x = ~fct_reorder(product_name, count), y = ~count, type = "bar", color= "darkgreen", colors = "viridis") |> 
  layout(xaxis = list(title = "Ice Cream Name", tickangle = -45), 
         yaxis = list(title = "Count"))
```

## Column {data-width="350"}

### Ice cream orders through the day, by weekday

```{r, echo=TRUE}
instacart |> 
  filter(str_detect(product_name, "Ice Cream")) |> 
  group_by(order_hour_of_day, order_dow)  |> 
  summarise(count = n()) |> 
  mutate(weekday = case_match(
    order_dow, 
    0 ~ "Sunday", 
    1 ~ "Monday", 
    2 ~ "Tuesday", 
    3 ~ "Wednesday", 
    4 ~ "Thursday", 
    5 ~ "Friday", 
    6 ~ "Saturday"
    )) |> 
  plot_ly(y = ~count, x = ~order_hour_of_day, type = "scatter", mode= "line", line = list(shape = "spline"), color = ~weekday, colors = "viridis") |> 
  layout(xaxis = list(title = "Order Hour of Day", tickangle = -45), 
         yaxis = list(title = "Units Ice Cream Ordered"))
```

### Order size compared to when ice cream was added to cart

```{r, echo=TRUE}
plot_cart <- instacart |> 
  group_by(order_id) |> 
  mutate(total_items = n()) |> 
  filter(str_detect(product_name, "Ice Cream")) |> 
  ggplot(aes(x = total_items,
             y = add_to_cart_order)) +
  geom_point(color= "darkorchid4", alpha = 0.5) +
  xlab("Total items in order") +  
  ylab("Order of Ice Cream to Cart") + 
  theme(legend.position = "none")


ggplotly(plot_cart)
```